// const { legacy_createStore } = require("redux");

// console.log(redux);
/*
  {
    __DO_NOT_USE__ActionTypes: {
      INIT: '@@redux/INIT3.8.j.8.l.j',
      REPLACE: '@@redux/REPLACEa.7.z.i.2',
      PROBE_UNKNOWN_ACTION: [Function: PROBE_UNKNOWN_ACTION]
    },
    
    applyMiddleware是用来在redux里面做异步的
    applyMiddleware: [Function: applyMiddleware],

    bindActionCreators: [Function: bindActionCreators],

    combineReducers是用来在redux里面做模块化的
    combineReducers: [Function: combineReducers],

    compose: [Function: compose],

    createStore是用来创建仓库的
    createStore: [Function: createStore],

    新版本要用legacy_createStore来代替createStore来创建仓库
    legacy_createStore: [Function: createStore]
  }
*/

const { legacy_createStore } = require("redux");

// initState就是仓库数据
const initState = {
  count: 10,
  name: "zhangsan",
};

// reducer是一个函数，用来修改仓库的state的
// 里面有两个参数，分别是state和action
// action是一个带有type属性的对象
// reducer是能去return一个新的对象，然后覆盖原来的老对象
const reducer = (state = initState, action) => {
  switch (action.type) {
    case "add":
      return {
        ...state,
        count: state.count + 1,
      };
    case "minus":
      return {
        ...state,
        count: state.count - action.n,
      };
    case "changeName":
      return {
        ...state,
        name: action.name,
      };
    default:
      return state;
  }
};

// 调用legacy_createStore方法可以返回一个仓库实例
// 至少需要一个参数reducer函数
const store = legacy_createStore(reducer);
// console.log(store);

/*
  store仓库实例
  {
    dispatch是用来调用reducer方法的并且修改仓库数据的
    dispatch: [Function: dispatch],

    subscribe表示订阅，监听仓库的数据的改变的
    subscribe: [Function: subscribe],

    getState用来获取仓库数据的
    getState: [Function: getState],
    replaceReducer: [Function: replaceReducer],
    '@@observable': [Function: observable]
  }
*/

// console.log(store.getState());

// dispatch可以传入一个参数，就是action对象
// store.dispatch({ type: "add" });
// console.log(store.getState());

// store.dispatch({ type: "minus", n: 2 });
// console.log(store.getState());

// store.dispatch({ type: "changeName", name: "wangwu" });
// console.log(store.getState());

store.subscribe(() => {
  console.log(store.getState());
});

store.dispatch({ type: "add" });
store.dispatch({ type: "minus", n: 2 });
store.dispatch({ type: "changeName", name: "wangwu" });
